package com.xhldtc.proxy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.sina.sae.fetchurl.SaeFetchurl;

public class MainServlet extends HttpServlet
{
	private static final Logger log = Logger.getLogger(MainServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		System.setProperty("http.proxyHost", "10.240.45.225");
		System.setProperty("http.proxyPort", "80");
		
		String uri = URLDecoder.decode(req.getParameter("uri"), "UTF-8");

		DefaultHttpClient client = new DefaultHttpClient();
		HttpGet hg = new HttpGet(uri);

		Enumeration<String> enums = req.getHeaderNames();
		while (enums.hasMoreElements())
		{
			String headName = enums.nextElement();
			hg.setHeader(headName, req.getHeader(headName));
		}

		HttpResponse response = client.execute(hg);

		PrintWriter out = resp.getWriter();

		out.write(EntityUtils.toString(response.getEntity()));
		out.flush();
		out.close();
	}
}
